﻿/**
 * FPS Counter
 *
 * @author		Duarte Peixinho
 * @version		1.0
 */

package com.duartepeixinho.fps
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	import flash.system.System;
	import flash.text.TextFormat;
	
	public class FpsCounter extends Sprite 
	{
		
		private var _textFps:TextField = new TextField();
		private var _textMem:TextField = new TextField();
		private var _timer:Timer = new Timer(1000);
		private var _ticks:int = 0;
		private var _graphicFps:Shape = new Shape();
		private var _graphicMem:Shape = new Shape();
		private var _stageDefault:Stage;
		private var _graphicColor:uint = 0;
		
		/**
		 * Fps Counter Constructor
		 * @param	stage				Stage		Stage where fps will be applied
		 * @param	backGroundColor		uint		Background Color
		 * @param	outLineColor		uint		OutLine Color
		 * @param	graphicColor		uint		Graphic Color
		 * @param	fontSize			uint		Font Size
		 * @param	fontColor			uint		Font Color
		 * @param	fontType			String		Font Name
		 */
		public function FpsCounter(stage:Stage, backGroundColor:uint = 0xFFFFCC, outLineColor:uint = 0xFFFFCC, graphicColor:uint = 0xff0000, fontSize:uint=9, fontColor:uint=0x000000,fontType:String="verdana" ):void 
		{
			_stageDefault = stage;
			_graphicColor = graphicColor;
			
			graphics.lineStyle(1,outLineColor);
			graphics.beginFill(backGroundColor);
			graphics.drawRect(1, 1, 100, 30);
			
			var style:TextFormat = new TextFormat();
			style.font = fontType;
			style.size = fontSize;
			style.color = fontColor;
			
			_textFps.x = 5;
			_textFps.y = 2;
			_textFps.defaultTextFormat = style;
			_textFps.selectable = false;
			_textFps.text = "FPS: 0/" + stage.frameRate.toString() + "fps";
			addChild(_graphicFps);
			addChild(_textFps);			
			
			_textMem.defaultTextFormat = style;
			_textMem.selectable = false;
			_textMem.x = 5;
			_textMem.y = 15;
			_textMem.text = "MEM: 0 Mb";
			addChild(_graphicMem);
			addChild(_textMem);
			
			_textMem.height = _textMem.textHeight +5;
			_textFps.height = _textFps.textHeight +5;
			
			graphics.endFill();
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER, tick);
			addEventListener(Event.ENTER_FRAME, processFrame);
		}
		/**
		 * Loop Function		 
		 */
		private function processFrame(e:Event):void {
			_ticks++;
		}
		
		/**
		 * Timer
		 */
		private function tick(e:TimerEvent):void {
			if (_ticks > _stageDefault.frameRate) _ticks = _stageDefault.frameRate;
				_textFps.text = "FPS: " + _ticks + "/" + _stageDefault.frameRate.toString() + "fps";

			_graphicFps.graphics.clear();
			_graphicFps.graphics.beginFill(_graphicColor);
			_graphicFps.graphics.lineStyle(0);
			_graphicFps.graphics.drawRect(2, 4, (_ticks - 1) * 95 / _stageDefault.frameRate, 12);
			_graphicFps.graphics.endFill();
			_ticks = 0;
			_textMem.text = "MEM: " + Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';
		}
	}
	
}